package com.cqc.glide4demo;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.cqc.glide4demo.util.GlideApp;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.bumptech.glide.request.RequestOptions.centerCropTransform;//5
import static com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions.withCrossFade;//7

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.iv)
    ImageView iv;
    @BindView(R.id.gridView)
    GridView gridView;

    private Context context = this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        String[] array = getResources().getStringArray(R.array.texts);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, array);
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switchButton(position);
            }
        });
    }

    private void switchButton(int position) {
        switch (position) {
            case 0:
                GlideApp.with(context)
                        .load("http://mpic.tiankong.com/397/0e1/3970e127ca6491f17ad355feb2ff79d6/640.jpg")
                        .placeholder(R.mipmap.load)
                        .error(R.mipmap.error)
                        .fitCenter()
                        .into(iv);
                break;
            case 1:
                GlideApp.with(context).clear(iv);
                break;
            case 2:
                Glide.with(context)
                        .load("http://img3.duitang.com/uploads/blog/201410/19/20141019195610_WT5Sx.jpeg")
                        .into(iv);
                break;
            case 3:
                GlideApp.with(context)
                        .load("http://img5.imgtn.bdimg.com/it/u=268901695,3451826656&fm=11&gp=0.jpg")
                        .placeholder(new ColorDrawable(Color.RED))
                        .error(new ColorDrawable(Color.GREEN))
                        .centerCrop()
                        .into(iv);
                break;
            case 4://url 为null或“”
                GlideApp.with(context)
                        .load(null)
                        .placeholder(new ColorDrawable(Color.RED))
                        .error(new ColorDrawable(Color.GREEN))
                        .fallback(R.mipmap.error)
                        .centerCrop()
                        .into(iv);
                break;
            case 5:
                GlideApp.with(context)
                        .load("http://i0.hdslb.com/video/9b/9be9fb23e650a6f85f8a3c680562562b.jpg")
                        .apply(centerCropTransform())//import
                        .centerCrop()
                        .into(iv);
                break;
            case 6:
                RequestOptions options = new RequestOptions();
                options.centerCrop();
                GlideApp.with(context)
                        .load("http://02.imgmini.eastday.com/mobile/20170404/c04e1b6db721a6804f2a81ee19d207f5_wmk.jpeg")
                        .apply(options)
                        .centerCrop()
                        .into(iv);
                break;
            case 7:
                GlideApp.with(context)
                        .load("http://img0.imgtn.bdimg.com/it/u=2393875607,1448975941&fm=11&gp=0.jpg")
                        .transition(withCrossFade())
                        .centerCrop()
                        .into(iv);
                break;
        }
    }
}
